<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="mini" :inline="true" v-show="showSearch" label-width="80px">
      <el-form-item label="任务名称" prop="taskName">
        <el-input
          v-model="queryParams.taskName"
          placeholder="请输入任务名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="计划日期" prop="planDate">
        <el-date-picker clearable
          v-model="queryParams.planDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择计划执行日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['submit:plan:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="planList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="计划日期" align="center" prop="planDate" />
      <el-table-column label="任务名称" align="center" prop="taskPlanName" />
      <el-table-column label="执行状态" align="center" prop="taskStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.submit_plan_status" :value="scope.row.taskStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="计划执行时间" align="center" prop="planExecureTime"/>
      <el-table-column label="实际执行时间" align="center" prop="actualStartTime"/>
      <el-table-column label="实际结束时间" align="center" prop="actualStopTime"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-zoom-in"
            @click="detailQuery(scope.row)"
            v-hasPermi="['submit:plan:edit']"
          >详情</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-thumb"
            @click="handleSubmit(scope.row)"
            v-hasPermi="['submit:plan:remove']"
            v-if = "scope.row.taskStatus === '2' || scope.row.taskStatus === '5' 
            || scope.row.taskStatus === '7'"
          >手动报送</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-odometer"
            @click="handleCheck(scope.row)"
            v-if = "scope.row.taskStatus !== '1' && scope.row.taskStatus !== '8'"
            v-hasPermi="['submit:plan:remove']"
          >数据校验</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 查看报送详情对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1000px" append-to-body>
      <el-table v-loading="loading" :data="detailList">
      <el-table-column label="文件名称" align="center" prop="fileName" />
      <el-table-column label="数据开始日期" align="center" prop="dataStartDate" />
      <el-table-column label="数据结束日期" align="center" prop="dataEndDate" />
      <el-table-column label="状态" align="center" prop="produceStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.submit_plan_status" :value="scope.row.produceStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="计划执行时间" align="center" prop="planProduceTime"/>
      <el-table-column label="实际执行时间" align="center" prop="actureProduceTime"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-zoom-in"
            v-if ="scope.row.produceStatus !== '1'"
            @click="handleDataQuery(scope.row)"
            v-hasPermi="['submit:plan:edit']"
          >查看数据</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-message"
            v-if ="scope.row.produceStatus === '1'"
            @click="handleImport(scope.row)"
          >手工填报</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-message"
            v-if ="scope.row.produceStatus === '4' || scope.row.produceStatus === '6' 
            || scope.row.produceStatus === '7' || scope.row.produceStatus === '8'"
            @click="handleDownloadFile(scope.row)"
          >下载</el-button>
        </template>
      </el-table-column>
    </el-table>
    </el-dialog>

    <!-- 预览弹出框 -->
    <<el-dialog :visible.sync="previewDialogVisible" title="预览数据" width="80%">  
      <el-table    
        v-if="previewData && previewData.data && previewData.data.length > 0"    
        :data="previewData.data"    
        :stripe="true"    
        max-height="500px"    
      >    
        <el-table-column    
          v-for="column in previewData.columns"    
          :key="column"    
          :prop="column"    
          :label="column"    
          :min-width="150"    
        >  
          <template slot-scope="scope">  
            <span :class="{ 'highlight-class': scope.row.checkResult[column] }">  
              {{ scope.row[column] }}  
            </span>  
          </template>  
        </el-table-column>    
      </el-table>  
      <div v-else>暂无数据</div>  
    </el-dialog>

    <!-- 文件上传对话框 -->  
    <el-dialog  
    :visible.sync="importDialogVisible"  
    title="手工填报"  
    width="40%"  
    :close-on-click-modal="false"   
    :modal="true"  
    :modal-append-to-body="true"  
    custom-class="import-dialog"  
  >  
    <div class="dialog-content">
      <div class="prompt-text">请先下载模板文件，填报完成后再上传</div>   
      <div class="action-row">  
        <el-button  
          :size="'mini'"  
          @click="handleDownload"  
          class="download-button"  
        >  
          下载模板  
        </el-button>  
        <file-upload  
          v-model="submitDataImportVo.fileStorageId"  
          class="file-upload"  
        />  
      </div> 
  
      <div class="dialog-button-group" style="margin-top: 20px;">  
        <el-button  
          type="primary"  
          @click="submitDataImport"  
          class="submit-button"  
        >  
          提交  
        </el-button>    
      </div>  
    </div>  
  </el-dialog> 
  </div>
</template>



<script>
import { listPlan, getPlan, check, handSubmit,queryData,importData,downloadTemplate} from "@/api/submit/plan";

export default {
  name: "Plan",
  dicts: ['submit_plan_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 报送任务计划表格数据
      planList: [],
      // 详情表格数据
      detailList: [],
      // 压缩文件ID
      compressFileId: null,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        taskName: null,
        planDate: null,
      },
      // 表单参数
      form: {},
      submitPlanDataCheckVo: {
        taskPlanId: null,
        planDetailId: null
      },
       // 预览弹窗显示控制
       previewDialogVisible: false,
       // 预览数据
       previewData:{
        
       },
       //填报弹窗显示控制
       importDialogVisible: false,
       // 填报参数
       submitDataImportVo:{
        planDetailId: null,
        fileStorageId: null
       },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询报送任务计划列表 */
    getList() {
      this.loading = true;
      listPlan(this.queryParams).then(response => {
        this.planList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        taskPlanId: null,
        taskId: null,
        planExecureTime: null,
        taskStatus: null,
        createTime: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.taskPlanId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 详情按钮操作 */
    detailQuery(row) {
      this.reset();
      const taskPlanId = row.taskPlanId || this.ids
      getPlan(taskPlanId).then(response => {
        this.detailList = response.data.list;
        this.open = true;
        this.title = "查看详情计划";
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const taskPlanIds = row.taskPlanId || this.ids;
      this.$modal.confirm('是否确认删除报送任务计划编号为"' + taskPlanIds + '"的数据项？').then(function() {
        return delPlan(taskPlanIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('submit/plan/export', {
        ...this.queryParams
      }, `plan_${new Date().getTime()}.xlsx`)
    },
    handleCheck(row) {
      this.submitPlanDataCheckVo.taskPlanId = row.taskPlanId;
      this.submitPlanDataCheckVo.planDetailId = row.planDetailId;
      check(this.submitPlanDataCheckVo).then(response => {
        if(response.code == 200){
          this.$modal.msgSuccess("校验成功");
        }else{
          this.$modal.msgError(response.msg);
        }
      });
    },
    handleSubmit(row) {
      handSubmit(row.taskPlanId).then(response => {
        if(response.code == 200){
          this.$modal.msgSuccess("手工报送成功");
        }else{
          this.$modal.msgError(response.msg);
        }
      });
    },
    handleDataQuery(row){
      queryData(row.planDetailId).then(response => {
        if(response.code == 200){
          this.previewData = response.data;
          console.log(this.previewData)
          this.previewDialogVisible = true; // 显示预览弹窗
        }else{
          this.$modal.msgError(response.msg);
        }
      });
    },
    handleImport(row){
      this.importDialogVisible = true;
      this.submitDataImportVo.planDetailId = row.planDetailId;
    },
    // 上传文件
    submitDataImport(){
      console.log(this.submitDataImportVo)
      if(this.submitDataImportVo.fileStorageId === null){
        this.$modal.msgError("请上传文件");
        return;
      }
      importData(this.submitDataImportVo).then(response => {
        if(response.code == 200){
          this.$modal.msgSuccess("导入成功");
          this.importDialogVisible = false;
        }else{
          this.$modal.msgError(response.msg);
        }
      });
    },
    // 文件下载处理
    handleDownload() {
      downloadTemplate(this.submitDataImportVo.planDetailId).then(response => {
        if(response.code == 200){
          this.$download.name(response.data.fileName,true);
        }else{
          this.$modal.msgError(response.msg);
        }
      });
    },
    handleDownloadFile(row){
      this.$download.name(row.fileId,false);
    },
  }
};
</script>
<style scoped>  
.import-dialog {  
  /* 自定义弹窗样式 */  
  border-radius: 8px;  
  overflow: hidden;  
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);  
}  
  
.dialog-content {  
  padding: 20px;  
}  
  
.dialog-button-group {  
  display: flex;  
  justify-content: flex-end;  
}  
  
.submit-button {  
  margin-right: 10px;  
}  
  
.download-button {  
  margin-right: 10px;  
}
.prompt-text {  
  margin-bottom: 10px;  
  font-size: 14px;  
  color: #606266;  
}
.highlight-class {  
  color: red; 
}  
.action-row {  
  display: flex;  
  /* justify-content: space-between;   */
  align-items: center;  
} 
</style>